/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ar.com.s21.poo.dao;

import ar.com.s21.poo.exception.GestionarException;
import ar.com.s21.poo.model.DetalleVenta;
import ar.com.s21.poo.model.Venta;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Other
 */
public class VentaDAO {

    /**
     *
     * @param venta
     * @throws SQLException
     */
    public void guardarVenta(Venta venta) throws SQLException {
        
        PreparedStatement preparedStatement = null;
        
        String insertVenta = "INSERT INTO VENTA(fecha_venta, id_cliente,id_vendedor,monto_total,id_tipo_pago,nro_factura) VALUES (?,?,?,?,?,?)";

        try {

            preparedStatement = MySQLConnection.getInstance().preparedStatement(insertVenta);


            preparedStatement.setDate(1, venta.getFechaVenta());
            preparedStatement.setInt(2, venta.getCliente().getIdUsuario());
            preparedStatement.setInt(3, venta.getVendedor().getIdUsuario());
            preparedStatement.setDouble(4, venta.getMontoTotal());
            preparedStatement.setInt(5, venta.getTipoPago().getIdTipoPago());
            preparedStatement.setInt(6,venta.getNroFactura());
            
            preparedStatement.execute();
            
            insertarDetalleFactura(venta);
            
            

        } catch (SQLException e) {
           
            System.out.println(e.getMessage());

        } finally {

            if (preparedStatement != null) {
                preparedStatement.close();
            }
        }
        
        
        
    }

    private void insertarDetalleFactura(Venta venta) throws SQLException {
        PreparedStatement preparedStatement = null;
        
        String insertVenta = "INSERT INTO detalleventa(id_producto,id_venta, cantidad, precio_unitario) VALUES (?,?,?,?)";

        try {

            preparedStatement = MySQLConnection.getInstance().preparedStatement(insertVenta);
            int idVenta=obtenerIdVenta(venta.getNroFactura());

            for (DetalleVenta dv : venta.getDetalleVenta()) {
                
                preparedStatement.setInt(1, dv.getProducto().getId_producto());
                preparedStatement.setInt(2, idVenta);
                preparedStatement.setInt(3, dv.getCantidad());
                preparedStatement.setDouble(4,dv.getProducto().getPrecio());
                preparedStatement.execute();         
            }
            
        

        } catch (SQLException e) {
           
            System.out.println(e.getMessage());

        } finally {

            if (preparedStatement != null) {
                preparedStatement.close();
            }
        }
    }

    private int obtenerIdVenta(int nroFactura) throws SQLException {
        PreparedStatement preparedStatement = null;
        Integer idVenta= null;
        String sqlGetIdVenta = "select * from venta where nro_factura=?";

        try {

            preparedStatement = MySQLConnection.getInstance().preparedStatement(sqlGetIdVenta);
    
            
            preparedStatement.setInt(1, nroFactura);
            
            ResultSet result= preparedStatement.executeQuery();
            
            while(result.next()){
                idVenta = result.getInt("id_venta");
            }    
            

        } catch (SQLException e) {
           
            System.out.println(e.getMessage());

        } finally {

            if (preparedStatement != null) {
                preparedStatement.close();
            }
        }
        return idVenta;
    
    }
    
}
